package com.excuse.excusebuilder.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import com.excuse.excusebuilder.commen.util.NetWorkUtil;
import com.excuse.excusebuilder.commen.util.ToastUtil;


/**
 * Created by U-nookia on 2017/4/2.
 * 检测网络状态的广播接收器
 */

public class InternetConnectStatusReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        NetworkInfo activeNetwork = ((ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();

        if (!ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())
                ||activeNetwork == null
                || !activeNetwork.isConnected()) { // connected to the internet
            NetWorkUtil.netWorkConnected = false;
            ToastUtil.makeShortToast(context,"网络断开，请检查网络连接");
            return;
        }

        if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE){
            if (activeNetwork.isConnected()){
                NetWorkUtil.netWorkConnected = true;
                //ToastUtil.makeShortToast(context,"网络已连接");
                return;
            }
        }
        if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
            if (activeNetwork.isConnected()){
                //initChectInternetRetrofit(context);
                NetWorkUtil.netWorkConnected = true;
            }else {
                NetWorkUtil.netWorkConnected = false;
                ToastUtil.makeShortToast(context,"网络断开，请检查网络连接");
            }
        }
    }
}
